<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" class="addbtn" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格 -->
      <el-table :data="goodslist" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150px">
          <template slot-scope="scope">{{scope.row.add_time | dateForm}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 编辑商品对话框 -->
      <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%" @close="closeDialog">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="editForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editForm.goods_price"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="uploadGoods">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询数据参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 数据总数
      total: 0,
      // 获取数据信息的对象
      goodslist: [],
      uploadGoodsId: '',
      // 控制编辑对话框隐藏
      editDialogVisible: false,
      // 获取当前编辑商品数据
      editForm: {},
      // 验证规则
      editFormRules: {
        goods_name: [
          {
            required: true,
            message: '请输入商品名称',
            triggle: 'blur'
          }
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量',
            triggle: 'blur'
          }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            triggle: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 请求商品数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: {
          query: this.queryInfo.query,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize
        }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('数据请求失败！')
      }
      this.goodslist = res.data.goods
      this.total = res.data.total
    },
    // 分页显示数量改变时
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    // 当前分页改变时
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsList()
    },
    // 删除商品
    async delGood(id) {
      const confirmRes = await this.$confirm(
        '此操作将永久删除商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmRes !== 'confirm') return
      const { data: res } = await this.$http.delete(`goods/${id}`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      this.getGoodsList()
    },
    // 编辑商品
    async editGoods(id) {
      const { data: res } = await this.$http.get(`goods/${id}`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败！')
      }
      this.editForm = res.data
      this.uploadGoodsId = id
      console.log(id)
      this.editDialogVisible = true
    },
    // 上传商品修改
    async uploadGoods() {
      const { data: res } = await this.$http.put(
        `goods/${this.uploadGoodsId}`,
        this.editForm
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('商品信息修改失败！')
      }
      this.$message.success('商品信息修改成功！')
      this.getGoodsList()
      this.editDialogVisible = false
    },
    // 监听对话框，关闭后清空表单
    closeDialog() {
      this.$refs.editFormRef.resetFields()
    },
    // 添加商品
    addGoods() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.addbtn {
  margin-left: 10px;
}
</style>
